package mfuny.ggongal.admin.policy;

import java.util.HashMap;
import java.util.List;

import mfuny.ggongal.admin.Server;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.springframework.web.servlet.ModelAndView;

import com.ibatis.sqlmap.client.SqlMapClient;

public class PolicyService {
	SqlMapClient sqlMapper;
	ModelAndView mav;
	
	public PolicyService() throws Exception {
		sqlMapper = Server.getSqlMapper();
		mav = new ModelAndView();
	}
	
	//운영정책 목록.
	public ModelAndView policyList() throws Exception {
		mav.clear();
		mav.setViewName("/policy/policyList.ga");
		return mav;
	}
	
	//운영정책 리스트 가져오기.
	@SuppressWarnings("unchecked")
	public ModelAndView getPolicyList(String param) throws Exception {
		JSONObject jsonParam = (JSONObject) JSONValue.parse(param);
		int pageNum = jsonParam.get("num") instanceof Integer ? (Integer)jsonParam.get("num") : (int)(long)(Long)jsonParam.get("num");
		int pageSize = jsonParam.get("pageSize") instanceof Integer ? (Integer)jsonParam.get("pageSize") : 
					(int)(long)(Long)jsonParam.get("pageSize");
				
		int num = pageNum == 0 ? 1 : pageNum * pageSize - pageSize;
		jsonParam.put("num", num);
		
		List<HashMap<String, Object>> resultList = sqlMapper.queryForList("policy_selectList", jsonParam);
		int totalCount = (Integer)sqlMapper.queryForObject("policy_totalCount", jsonParam);
		
		mav.clear();
		mav.setViewName("/policy/getPolicyList");
		mav.addObject("resultList", resultList);
		mav.addObject("totalCount", totalCount);
		mav.addObject("pageNum", pageNum);
		return mav;
	}
	
	//운영정책 등록 폼.
	public ModelAndView regPolicyForm() throws Exception {
		mav.clear();
		mav.setViewName("/policy/regPolicyForm.ga");
		return mav;
	}
	
	//운영정책 등록 처리.
	public ModelAndView regPolicy(PolicyDao policyDao) throws Exception {
		policyDao.setContent(policyDao.getContent().replaceAll("\r\n", "\\\\n"));
		sqlMapper.update("policy_insert", policyDao);
		return policyList();
	}
	
	//운영정책 글 내용 보기.
	public ModelAndView readPolicy(int policy_no) throws Exception {
		PolicyDao policyDao = (PolicyDao) sqlMapper.queryForObject("policy_selectByNo", policy_no);
		policyDao.setContent(policyDao.getContent().replaceAll("\\\\n", "\n"));
		mav.clear();
		mav.setViewName("/policy/readPolicy.ga");
		mav.addObject("resultClass", policyDao);
		return mav;
	}
	
	//운영정책 글 수정 폼.
	public ModelAndView modifyPolicyForm(int policy_no) throws Exception {
		PolicyDao policyDao = (PolicyDao) sqlMapper.queryForObject("policy_selectByNo", policy_no);
		policyDao.setContent(policyDao.getContent().replaceAll("\\\\n", "\n"));
		mav.clear();
		mav.setViewName("/policy/modifyPolicyForm.ga");
		mav.addObject("resultClass", policyDao);
		return mav;
	}
	
	//운영정책 글 수정 처리.
	public ModelAndView modifyPolicy(PolicyDao policyDao) throws Exception {
		policyDao.setContent(policyDao.getContent().replaceAll("\r\n", "\\\\n"));
		sqlMapper.update("policy_update", policyDao);
		return policyList();
	}
}
